package com.sunyur.tailor.channel.common.middle.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.HashMap;

/**
 * @Author 99288
 * @Description http远程调用工具
 **/
@Configuration
public class RestTemplateUtil<T> {
    private static final Logger LOGGER_INFO = LoggerFactory.getLogger(RestTemplateUtil.class);
    private static final Logger LOGGER_ERROR = LoggerFactory.getLogger(RestTemplateUtil.class);

    @Autowired
    private RestTemplate restTemplate;

    public JSONObject exchangeFormUrlencodedPost(T t, String url) {
        URI uri = URI.create(url);
        MultiValueMap<String, Object> postParameters = new LinkedMultiValueMap<>();
        HashMap<String, Object> hashMap = JSON.parseObject(JSON.toJSONString(t), HashMap.class);
        hashMap.forEach((k, v) -> postParameters.add(k, v.toString()));
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", MediaType.APPLICATION_FORM_URLENCODED_VALUE);
        HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(postParameters, headers);
        LOGGER_INFO.info("exchangeFormUrlencodedPost.param:{}", JSON.toJSONString(httpEntity));
        ResponseEntity<JSONObject> jsonObjectResponseEntity = null;
        JSONObject responseEntity = null;
        try {
            jsonObjectResponseEntity = restTemplate.postForEntity(uri, httpEntity, JSONObject.class);
            if (jsonObjectResponseEntity.getStatusCodeValue() == HttpStatus.OK.value()) {
                responseEntity = jsonObjectResponseEntity.getBody();
            }
        } catch (RestClientException e) {
            LOGGER_ERROR.error("exchangeFormUrlencodedPost.exception：{}", e);
        }
        LOGGER_INFO.info("exchangeFormUrlencodedPost.return:{}", JSON.toJSONString(jsonObjectResponseEntity));
        return responseEntity;
    }

    public JSONObject exchangeJsonPost(T t, String url) {
        URI uri = URI.create(url);
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", MediaType.APPLICATION_JSON_VALUE);
        HttpEntity<T> httpEntity = new HttpEntity<>(t, headers);
        LOGGER_INFO.info("exchangeJsonPost.param:{},url:{}", JSON.toJSONString(httpEntity), url);
        ResponseEntity<String> stringResponseEntity;
        JSONObject responseEntity = null;
        try {
            stringResponseEntity = restTemplate.postForEntity(uri, httpEntity, String.class);
            LOGGER_INFO.info("exchangeJsonPost stringResponseEntity is {}", JSON.toJSONString(stringResponseEntity));
            if (stringResponseEntity.getStatusCodeValue() == HttpStatus.OK.value()) {
                responseEntity = JSON.parseObject(stringResponseEntity.getBody());
            }
        } catch (RestClientException e) {
            LOGGER_ERROR.error("exchangeJsonPost.exception：{}", e);
        }
        LOGGER_INFO.info("exchangeJsonPost.return:{}", JSON.toJSONString(responseEntity));
        return responseEntity;
    }

    public JSONObject exchangeJsonGet(String url) {
        LOGGER_INFO.info("exchangeGet.param:{}", url);
        ResponseEntity<String> stringResponseEntity;
        JSONObject responseEntity = null;
        try {
            stringResponseEntity = restTemplate.getForEntity(url, String.class);
            LOGGER_INFO.info("exchangeGet.return:{}", JSON.toJSONString(stringResponseEntity));
            if (stringResponseEntity.getStatusCodeValue() == HttpStatus.OK.value()) {
                responseEntity = JSON.parseObject(stringResponseEntity.getBody());
            }
        } catch (RestClientException e) {
            LOGGER_ERROR.error("exchangeGet.exception：{}", e);
        }
        LOGGER_INFO.info("exchangeGet.return:{}", JSON.toJSONString(responseEntity));
        return responseEntity;
    }
}